/**
 * 商机主题维护 编辑页面
 * @author mengyb
 * @date 2019/04/10
 */
define(function () {
    var self,
        app=win.app,
        doc=win.doc,
        vueData={
            isAdd:false,
            isDetail:false,
            form:'',
            rules:{
                GWBG001:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBGL003:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBGL004:[
                    {required:true,message:'不可为空',trigger:'blur'}
                ],
                GWBGACTI:[
                    {required:true,message:'不可为空',trigger:'change'}
                ]
            },
            selectOptions:{
                sys_y_n:app.datas_('sys_y_n'),
                base_gwat152_gwbg003:app.datas_('base_gwat152_gwbg003'),
                base_gwat152_gwbg004:app.datas_('base_gwat152_gwbg004'),
            }
        },
        userList=[],//业务员列表
        gwakList=[],//客户列表
        callobj,
        pageVue,
        layOpenIndex,
        moduleName
    ;


    var Gwat152Edit=app.Module.extend({

        //初始化
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat152_edit',
                _loadTplsFull:[
                    {dom:'common_open_window',tpl:'common/_open_window'}
                ]
            }, win);
        },

        //开窗后回调函数
        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            moduleName=tagData.moduleName;
            layOpenIndex=tagData.layOpenIndex;
            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (tagData.isAdd){
                vueData.isAdd=true;
                app.ajax({
                    xurl:'base/gwat152/getGwbg005',//点击新增按钮，获取默认归属业务员
                    cb: function (result) {
                        if (result.ok) {
                            app.extendUpLow({
                                target:vueData.form,
                                res:{
                                    GWBG001:result.data.data.GWBG001,//商机主题编号
                                    GWBGL003:'',//说明
                                    GWBGL004:'',//助记码
                                    GWBG002:'',//客户编号
                                    GWBG002_NAME:'',//客户名称
                                    GWBG005:result.data.data.GWBG005,//业务员编号
                                    GWBG005_NAME:result.data.data.GWBG005_NAME,//归属业务员
                                    GWBG003:'Y',//特价否
                                    GWBG004:'Y',//信用否
                                    GWBGACTI:'Y'//有效否
                                },
                                reset:true
                            })
                        }

                    }
                });
            }else {
                vueData.isAdd=false;
                callobj=tagData.obj;
                app.extendUpLow({
                    target:vueData.form,
                    res: callobj.data,
                    reset:true
                });
            }
        },

        //新增
        add:function(){
            app.ajax({
                xurl:'base/gwat152/add',
                xdata:vueData.form,
                loading:true,
                okmsg:moduleName+'信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule("gwat152").reloadTableCurr();
                        layer.close(layOpenIndex);
                    }

                }
            })
        },

        //修改
        edit: function () {
            app.ajax({
                xurl:'base/gwat152/edit',
                xdata:vueData.form,
                loading:true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        callobj.update(result.xdata);
                        layer.close(layOpenIndex);
                    }
                }
            })
        },

        //DOM加载完成后调用
        _ready: function () {

            vueData.form=app.fields_(['base_gwat152']);

            //实例化vue
            pageVue = new Vue({
                el:'#gwat152_edit',
                data:vueData,
                methods:{
                    //关闭窗口
                    cancle: function () {
                        layer.close(layOpenIndex);
                    },
                    //保存
                    save: function () {
                        //表单校验
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if(vueData.isAdd){
                                    self.add();
                                }else{
                                    self.edit();
                                }
                            }

                        });

                    },
                    //检查开窗输入框值是否存在
                    check: function (type) {
                        if(type == 'GWBG002'){
                            if(vueData.form.GWBG002 != ''){
                                var obj={};
                                obj = gwakList.find(function (item) {
                                    return item.GWAK001 === vueData.form.GWBG002;
                                });
                                if (typeof obj == 'undefined') {
                                    vueData.form.GWBG002='';
                                    vueData.form.GWBG002_NAME='';
                                }else{
                                    vueData.form.GWBG002_NAME=obj.GWAKL003;
                                }
                            }else{
                                vueData.form.GWBG002_NAME='';
                            }
                        }else if(type == 'GWBG005_NAME'){
                            if(vueData.form.GWBG005_NAME != ''){
                                var obj={};
                                obj = userList.find(function (item) {
                                    return item.NAME === vueData.form.GWBG005_NAME;
                                });
                                if (typeof obj == 'undefined') {
                                    vueData.form.GWBG005='';
                                    vueData.form.GWBG005_NAME='';
                                }else{
                                    vueData.form.GWBG005=obj.ID;
                                }
                            }else{
                                vueData.form.GWBG005='';
                            }
                        }
                    },

                    //客户开窗
                    openKh: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwak001_1',
                                    columnList: [
                                        { label: '客户编号', prop: 'GWAK001', width: '150'},
                                        { label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        { label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWBG002=data.GWAK001;
                                    vueData.form.GWBG002_NAME=data.GWAKL003;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '470px']
                            }
                        });
                    },

                    //业务员开窗
                    openYwy: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [{label: '业务员编号', prop: 'ID', width: '150'},
                                        {label: '业务员名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWBG005=data.ID;
                                    vueData.form.GWBG005_NAME=data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择业务员',
                                area: ['600px', '470px']
                            }
                        });

                    },

                    clearGwbg002: function () {
                        vueData.form.GWBG002_NAME='';
                    }

                },
                //使用ElementUI初始化DOM
                created: function () {
                    self._setEleIsReady('gwat152_edit');
                }
            })

        },

        _lazy: function () {
            app.ajax({
                xurl:'base/gwat152/getGwakList',
                cb: function (result) {
                    gwakList=result.data.list;

                }
            });
            app.ajax({
                xurl:'base/gwat152/getUserList',
                cb: function (result) {
                    userList=result.data.list;

                }
            });
            this._super();
        }

    });



    return new Gwat152Edit();

});
